package com.ljm.mp.http;

import com.ljm.mp.exception.MpException;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;

public class HttpUtil {
    public static String httpGet(String url) {
        try {
            HttpGet httpGet=new HttpGet(url);
            CloseableHttpClient httpClient= HttpClientBuilder.create().build();
            CloseableHttpResponse response=httpClient.execute(httpGet);
            int status=response.getStatusLine().getStatusCode();
            return 200==status ? EntityUtils.toString(response.getEntity()) : null ;
        } catch (IOException e) {
            throw new MpException(e.getCause());
        }
    }

    public static String httpPost(String url,String jsonStr){
        try {
            CloseableHttpClient httpClient= HttpClientBuilder.create().build();
            HttpPost httpPost=new HttpPost();
            httpPost.setURI(URI.create(url));
            if(jsonStr!=null&&!"".equals(jsonStr)){
                HttpEntity entity=new StringEntity(jsonStr,"UTF-8");
                httpPost.setEntity(entity);
            }
            CloseableHttpResponse response=httpClient.execute(httpPost);
            int status=response.getStatusLine().getStatusCode();
            return 200==status ? EntityUtils.toString(response.getEntity()) : null ;
        } catch (IOException e) {
            throw new MpException(e.getCause());
        }
    }

    public static void main(String[] str ) throws IOException {
        //String data=HttpUtil.httpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx80f594cd37e91d03&secret=551de1d91025bfdc27a078ce638a1773");
        //System.out.println(data);
        String jsonStr="{\n" +
                "     \"button\":[\n" +
                "     {    \n" +
                "          \"type\":\"click\",\n" +
                "          \"name\":\"今日歌曲\",\n" +
                "          \"key\":\"V1001_TODAY_MUSIC\"\n" +
                "      },\n" +
                "      {\n" +
                "           \"name\":\"菜单\",\n" +
                "           \"sub_button\":[\n" +
                "           {    \n" +
                "               \"type\":\"view\",\n" +
                "               \"name\":\"搜索\",\n" +
                "               \"url\":\"http://www.soso.com/\"\n" +
                "            },\n" +
                "            {\n" +
                "                 \"type\":\"miniprogram\",\n" +
                "                 \"name\":\"wxa\",\n" +
                "                 \"url\":\"http://mp.weixin.qq.com\",\n" +
                "                 \"appid\":\"wx286b93c14bbf93aa\",\n" +
                "                 \"pagepath\":\"pages/lunar/index\"\n" +
                "             },\n" +
                "            {\n" +
                "               \"type\":\"click\",\n" +
                "               \"name\":\"赞一下我们\",\n" +
                "               \"key\":\"V1001_GOOD\"\n" +
                "            }]\n" +
                "       }]\n" +
                " }";
        String data=HttpUtil.httpPost("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=21_6O8uJRt2QDIXLYHghf7zYzEelKiOXTEdTb6BE7kYknnPDJZQIEqeBGpn5bwIunl_fj_VRTw7pLzt8Jhp7L4JN-NZZLPM806Ms_sWuEdHjbxcrVa5BbUd7amutbVzpuivMRqqHH0pqAYRm0r3DNJbABAQAW",
                jsonStr
                );
        System.out.println(data);
    }
}
